package app.musicPlayer.domain;

import java.util.ArrayList;
import java.util.List;



import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import app.persistencia.CancionLista;
import app.persistencia.ListasDeReproduccion;
import app.utils.Utiles;

public class SongsByListSelection extends ListActivity {
	
	ArrayAdapter<ModelCancion> adapter;
	Integer idListSelected;
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		// Create an array of Strings, that will be put to our ListActivity
		Bundle bundle = getIntent().getExtras();
        idListSelected = Integer.valueOf(bundle.getString("ID"));
        
		adapter = new InteractiveArrayAdapterSongsSelection(SongsByListSelection.this, getModelCancion());
		setListAdapter(adapter);
	}

	private List<ModelCancion> getModelCancion() {
		List<ModelCancion> list = new ArrayList<ModelCancion>();
		ArrayList<CancionLista> listaRep = ListasDeReproduccion.getIntances().getListaDeReproduccion(idListSelected).getListaRep();
		for(int i = 0; i < listaRep.size(); i++) {
			list.add(new ModelCancion(listaRep.get(i), false));
		}
		return list;
	}

	/*private ModelCancion get(String s) {
		return new ModelCancion();
	}
	*/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menuselection, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.BorrarSeleccion:
	            borrar();
	            return true;
	        case R.id.volver:
	        	returnToMain(null);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    private void returnToMain(Bundle b)
    {
    	Intent intent = new Intent(SongsByListSelection.this, SocialMusicPlayerActivity.class);
    	if(b != null) {
    		intent.putExtras(b);
    	}

    	finish();
    	startActivity(intent);

    }
    
	private void borrar() {
		AlertDialog alertDialog = new AlertDialog.Builder(SongsByListSelection.this).create();
    	alertDialog.setTitle("Borrar");
    	alertDialog.setMessage("¿Estas seguro que deseas borrar las canciones seleccionadas de la lista de reproduccion?");
 	    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    	      public void onClick(DialogInterface dialog, int which) {
    	 
    	  		  ListView list = getListView();

    	    	  for(int i = 0; i < list.getCount(); i++) {
    	    		  if(adapter.getItem(i).isSelected()) {
    	    	          //System.out.println("selected");
    	    			  String name = adapter.getItem(i).getCancion().getTittle();
    	    			  Integer pos = adapter.getItem(i).getCancion().getPosition();
    	    			  ListasDeReproduccion.getIntances().removeSong(SongsByListSelection.this,
    	    					  idListSelected, name, pos);
    	    		  }
    	    	  }
    	    	  Utiles.showMessage(SongsByListSelection.this, "Se ha borrado con exito");
    	      	  returnToMain(null);
    	    } });
     	 alertDialog.setButton2("CANCEL", new DialogInterface.OnClickListener() {
   	      public void onClick(DialogInterface dialog, int which) {
   	    	  
   	    } });
     	alertDialog.show();		
	}
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //moveTaskToBack(true);          
            returnToMain(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
	/**
	 * TODO FALLOS
	 * REVISAR ICONOS
	 */

}
